﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Sms.Model.V20160927;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace LPY.LeHealth.WebSite.Common
{
    /// <summary>
    /// 短信发送
    /// </summary>
    public class SmsHelper
    {
        /// <summary>
        /// 发送手机验证码
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        public static JsonMessage SendSecurityCode(string phone,string code)
        {
            JsonMessage msg = new JsonMessage();

            if (string.IsNullOrEmpty(phone))
            {
                msg.Code = 0;
                msg.Msg = "手机号码不能为空";
                return msg;
            }

            var accessKey = ConfigurationManager.AppSettings["Ali_AccessKey"];
            var secretKey = ConfigurationManager.AppSettings["Ali_SecretKey"];

            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKey, secretKey);
            IAcsClient client = new DefaultAcsClient(profile);
            SingleSendSmsRequest request = new SingleSendSmsRequest();
            try
            {
                var singName = ConfigurationManager.AppSettings["AliSms_SignName"];
                var templateCode = ConfigurationManager.AppSettings["AliSms_TemplateCode"];

                request.SignName = singName;
                request.TemplateCode = templateCode;
                request.RecNum = phone;

                request.ParamString = string.Format("{{\"code\":\"{0}\"}}", code);

                SingleSendSmsResponse httpResponse = client.GetAcsResponse(request);

                msg.Code = 1;
                msg.Msg = "验证码发送成功";
            }
            catch (ServerException e)
            {
                //服务端异常
                msg.Code = 0;
                msg.Msg = e.Message;

            }
            catch (ClientException e)
            {
                //客户端异常
                msg.Code = 0;
                msg.Msg = e.Message;
            }

            return msg;
        }
        /// <summary>
        /// 发送订单信息
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static JsonMessage SendDingInfor(string phone, string code)
        {
            JsonMessage msg = new JsonMessage();

            if (string.IsNullOrEmpty(phone))
            {
                msg.Code = 0;
                msg.Msg = "手机号码不能为空";
                return msg;
            }

            var accessKey = ConfigurationManager.AppSettings["Ali_AccessKey"];
            var secretKey = ConfigurationManager.AppSettings["Ali_SecretKey"];

            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKey, secretKey);
            IAcsClient client = new DefaultAcsClient(profile);
            SingleSendSmsRequest request = new SingleSendSmsRequest();
            try
            {
                var singName = ConfigurationManager.AppSettings["AliSms_SignName"];
                var templateCode = ConfigurationManager.AppSettings["AliSms_TemplateDingDan"];

                request.SignName = singName;
                request.TemplateCode = templateCode;
                request.RecNum = phone;

                request.ParamString = string.Format("{{\"code\":\"{0}\"}}", code);

                SingleSendSmsResponse httpResponse = client.GetAcsResponse(request);

                msg.Code = 1;
                msg.Msg = "订单消息发送成功";
            }
            catch (ServerException e)
            {
                //服务端异常
                msg.Code = 0;
                msg.Msg = e.Message;

            }
            catch (ClientException e)
            {
                //客户端异常
                msg.Code = 0;
                msg.Msg = e.Message;
            }

            return msg;
        }

        /// <summary>
        /// 发送注册信息
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public static JsonMessage SendRegisterInfo(string phone,string pwd)
        {
            JsonMessage msg = new JsonMessage();

            if (string.IsNullOrEmpty(phone))
            {
                msg.Code = 0;
                msg.Msg = "手机号码不能为空";
                return msg;
            }

            var accessKey = ConfigurationManager.AppSettings["Ali_AccessKey"];
            var secretKey = ConfigurationManager.AppSettings["Ali_SecretKey"];

            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKey, secretKey);
            IAcsClient client = new DefaultAcsClient(profile);
            SingleSendSmsRequest request = new SingleSendSmsRequest();
            try
            {
                var singName = ConfigurationManager.AppSettings["AliSms_SignName"];
                var templateRegister = ConfigurationManager.AppSettings["AliSms_TemplateRegister"];

                request.SignName = singName;
                request.TemplateCode = templateRegister;
                request.RecNum = phone;

                request.ParamString = string.Format("{{\"name\":\"{0}\",\"pwd\":\"{1}\"}}", phone, pwd);

                SingleSendSmsResponse httpResponse = client.GetAcsResponse(request);

                msg.Code = 1;
                msg.Msg = "验证码发送成功";
            }
            catch (ServerException e)
            {
                //服务端异常
                msg.Code = 0;
                msg.Msg = e.Message;

            }
            catch (ClientException e)
            {
                //客户端异常
                msg.Code = 0;
                msg.Msg = e.Message;
            }

            return msg;
        }
    }
}